package com.base.self.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.base.result.PageUtils;
import com.base.result.ResponseResult;
import com.base.result.ResponseResultUtils;
import com.base.self.common.SelfRuntimeException;
import com.base.self.dto.slideshow.ShowSlideshowDTO;
import com.base.self.dto.slideshow.SlideshowWebAddDTO;
import com.base.self.dto.slideshow.SlideshowWebDelDTO;
import com.base.self.dto.slideshow.SlideshowWebListDTO;
import com.base.self.entity.Slideshow;
import com.base.self.mapper.SlideshowMapper;
import com.base.self.service.ISlideshowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * <p>
 *  首页轮播图 服务实现类
 * </p>
 *
 * @author XiFYuW
 * @since 2020-11-17
 */
@Service
public class SlideshowServiceImpl extends ServiceImpl<SlideshowMapper, Slideshow> implements ISlideshowService {

    private final SlideshowMapper slideshowMapper;

    public SlideshowServiceImpl(SlideshowMapper slideshowMapper) {
        this.slideshowMapper = slideshowMapper;
    }

    @Override
    @Transactional(readOnly = true)
    public ResponseResult slideshowWebList(SlideshowWebListDTO slideshowWebListDTO) {
        final Map<String, Object> data = PageUtils.getDateMap(() -> page(PageUtils.getPage(
                new Page<>(), slideshowWebListDTO.getOffset(), slideshowWebListDTO.getLimit()),
                new QueryWrapper<Slideshow>()
                        .eq("is_del", 0)
                        .eq(slideshowWebListDTO.getSlideStatus() != null, "slide_status", slideshowWebListDTO.getSlideStatus())
                        .orderByDesc("create_time")));
        return ResponseResultUtils.getResponseResultS("查询成功", data);
    }

    @Override
    @Transactional
    public ResponseResult slideshowWebAdd(SlideshowWebAddDTO slideshowWebAddDTO) {
        final Slideshow slideshow = Slideshow
                .builder()
                .createTime(DateUtil.date())
                .slideName(slideshowWebAddDTO.getSlideName())
                .slideSrc(slideshowWebAddDTO.getSlideSrc())
                .slideStatus(1)
                .build();
        save(slideshow);
        return ResponseResultUtils.getResponseResultS("添加成功");
    }

    @Override
    @Transactional
    public ResponseResult slideshowWebDel(SlideshowWebDelDTO slideshowWebDelDTO) {
        final Slideshow slideshow = slideshowMapper.selectById(slideshowWebDelDTO.getId());
        Optional.ofNullable(slideshow).orElseThrow(() -> new SelfRuntimeException(ResponseResultUtils.getResponseResultF("首页轮播图错误")));
        slideshow.setIsDel(1);
        slideshow.setUpdateTime(DateUtil.date());
        updateById(slideshow);
        return ResponseResultUtils.getResponseResultS("删除成功");
    }

    @Override
    @Transactional(readOnly = true)
    public ResponseResult showSlideshow(ShowSlideshowDTO showSlideshowDTO) {
        List<Slideshow> list = list(new QueryWrapper<Slideshow>()
                .eq("is_del", 0)
                .eq("slide_status", 1)
                .orderByDesc("create_time")
        );
        return ResponseResultUtils.getResponseResultS("查询成功", list);
    }
}
